import 'package:flutter/material.dart';
import 'package:flutter_base/user/app_user_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/base/widget/base_get_builder.dart';
import 'package:flutter_base/widgets/appbar/common_app_bar.dart';
import 'package:flutter_base/widgets/image/circle_avatar_widget.dart';
import 'package:flutter_base/widgets/widget_line.dart';
import 'package:flutter_base/generated/assets.dart';
import 'package:flutter_base/widgets/image/auto_image.dart';
import 'package:flutter_base/config/app_color.dart';
import 'mine_logic.dart';

class MinePage extends StatelessWidget {
  final logic = Get.put(MineLogic());

  MinePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<MineLogic>(

      builder: (_) {
        return Container(
          decoration: const BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(Assets.mineBg),
                  fit: BoxFit.fitWidth,
                  alignment: Alignment.topCenter)),
          child: SingleChildScrollView(child: SafeArea(child: Obx(() {
            return Column(
              children: [
                // 用户信息卡片
                _buildUserCard(),

                SizedBox(height: 20.w),

                // 我的测试信息卡片
                _buildMyTestCard(),

                SizedBox(height: 24.w),

                // 常用服务
                _buildPopularServices(),

                SizedBox(height: 24.w),

                // 其他服务
                _buildOtherServices(),

                SizedBox(height: 40.w),
              ],
            );
          }))),
        );
      },
    );
  }

  // 用户信息卡片
  Widget _buildUserCard() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              GestureDetector(
                onTap: () => logic.onEditProfile(),
                child: Container(
                  padding:
                      EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(45.w),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      AutoImage(
                        Assets.mineEdit,
                        width: 16.w,
                      ),
                      SizedBox(width: 4.w),
                      Text(
                        'Edit',
                        style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                          color: AppColor.color0C0A09,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          6.verticalSpaceFromWidth,
          SizedBox(
            width: 72.w,
            height: 72.w,
            child: Stack(
              children: [
                // 头像
                CircleAvatarWidget(
                  imageUrl: AppUserManager().user.value?.avatar,
                  radius: 36.w,
                  borderColor: Colors.white,
                  borderWidth: 2.w,
                ),
                // 右下角编辑图标
                Positioned(
                    right: 0,
                    bottom: 0,
                    child: AutoImage(
                      Assets.mineTopVipTag,
                      width: 20.w,
                    )),
              ],
            ),
          ),
          // 头像部分
          //

          16.verticalSpaceFromWidth,
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 用户名
              Text(
                AppUserManager().user.value?.username ??
                    AppUserManager().user.value?.email ??
                    "",
                style: TextStyle(
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w600,
                  color: AppColor.color0C0A09,
                ),
              ),
              SizedBox(width: 4.w),
              // 性别标签
              Container(
                width: 20.w,
                height: 20.w,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(33.w),
                ),
                alignment: Alignment.center,
                child: AutoImage(
                  AppUserManager().user.value?.sex == 1
                      ? Assets.commonMale
                      : Assets.commonFemale,
                  width: 14.w,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // 我的测试卡片
  Widget _buildMyTestCard() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: GestureDetector(
        onTap: () => logic.onMyTestQuestions(),
        child: Container(
          padding: EdgeInsets.all(12.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.w),
          ),
          child: Row(
            children: [
              // 左侧图标
              AutoImage(
                Assets.mineUserEdit,
                width: 52.w,
                height: 52.w,
              ),

              SizedBox(width: 12.w),
              // 右侧信息
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      'My Test Questions',
                      style: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w600,
                        color: AppColor.color0C0A09,
                      ),
                    ),
                    8.verticalSpaceFromWidth,
                    Row(
                      children: [
                        Text(
                          'Completed:',
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w500,
                            color: AppColor.color7F909E,
                          ),
                        ),
                        SizedBox(width: 4.w),
                        Text(
                          '12',
                          style: TextStyle(
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w600,
                            color: AppColor.color0C0A09,
                          ),
                        ),
                        // SizedBox(width: 20.w),
                        const Spacer(),
                        Container(
                          width: 1.w,
                          height: 16.w,
                          color: AppColor.colorF5F7FA,
                        ),
                        const Spacer(),
                        // SizedBox(width: 20.w),
                        Text(
                          'Not Completed:',
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w500,
                            color: AppColor.color7F909E,
                          ),
                        ),
                        SizedBox(width: 4.w),
                        Text(
                          '3',
                          style: TextStyle(
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w600,
                            color: AppColor.color0C0A09,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              12.horizontalSpace,
              // 右侧箭头
              Container(
                width: 24.w,
                height: 24.w,
                decoration: const BoxDecoration(
                  color: AppColor.colorF5F7FA,
                  shape: BoxShape.circle,
                ),
                child: Center(
                  child: AutoImage(
                    Assets.commonItemArrow,
                    width: 16.w,
                    height: 16.w,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 常用服务
  Widget _buildPopularServices() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(left: 4.w),
            child: Text(
              'Popular Services',
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w700,
                color: AppColor.color0C0A09,
              ),
            ),
          ),
          SizedBox(height: 16.w),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.w),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(20.w),
            ),
            child: Column(
              children: [
                _buildServiceItem(
                  icon: Assets.mineVip,
                  title: 'VIP',
                  trailing: Container(
                    padding:
                        EdgeInsets.symmetric(horizontal: 10.w, vertical: 4.w),
                    decoration: BoxDecoration(
                      gradient: const LinearGradient(
                        colors: [Color(0xFFFFBA01), Color(0xFFFF0301)],
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                      ),
                      borderRadius: BorderRadius.circular(23.w),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          'Expires: 2025.05.20',
                          style: TextStyle(
                            fontSize: 10.sp,
                            fontWeight: FontWeight.w500,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                  onTap: () => logic.onVipTap(),
                ),
                _buildServiceItem(
                  icon: Assets.mineWallet,
                  title: 'My Wallet',
                  onTap: () => logic.onWalletTap(),
                ),
                _buildServiceItem(
                  icon: Assets.mineFavorite,
                  title: 'My Favorites',
                  onTap: () => logic.onFavoriteTap(),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 其他服务
  Widget _buildOtherServices() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Other Services',
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w700,
              color: AppColor.color0C0A09,
            ),
          ),
          SizedBox(height: 12.w),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.w),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(20.w),
            ),
            child: Column(
              children: [
                _buildServiceItem(
                  icon: Assets.mineFaq,
                  title: 'FAQ',
                  onTap: () => logic.onFaqTap(),
                ),

                // SizedBox(height: 24.w),
                _buildServiceItem(
                  icon: Assets.mineSettings,
                  title: 'Settings',
                  onTap: () => logic.onSettingTap(),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 服务项
  Widget _buildServiceItem({
    required String icon,
    required String title,
    Widget? trailing,
    required Function onTap,
  }) {
    return GestureDetector(
        onTap: () => onTap(),
        behavior: HitTestBehavior.opaque,
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 8.w),
          child: Row(
            children: [
              // 图标
              Container(
                width: 40.w,
                height: 40.w,
                decoration: BoxDecoration(
                  color: AppColor.colorF5F7FA,
                  borderRadius: BorderRadius.circular(20.w),
                ),
                child: Center(
                  child: AutoImage(
                    icon,
                    width: 24.w,
                    height: 24.w,
                    color: AppColor.color0C0A09,
                  ),
                ),
              ),

              SizedBox(width: 12.w),

              // 标题
              Text(
                title,
                style: TextStyle(
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w500,
                  color: AppColor.color0C0A09,
                ),
              ),

              const Spacer(),

              // 自定义尾部组件
              trailing ?? const SizedBox(),

              // 箭头
              SizedBox(width: 4.w),
              AutoImage(
                Assets.commonItemRight,
                width: 24.w,
                height: 24.w,
              ),
            ],
          ),
        ));
  }
}
